package com.auleev.string;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexFoo {

	private static final String DOMAIN_LABEL_REGEX = "\\p{Alnum}(?>[\\p{Alnum}-]*\\p{Alnum})*";
	private static final String TOP_LABEL_REGEX = "\\p{Alpha}{2,}";
	private static final String DOMAIN_NAME_REGEX = "^(?:" + DOMAIN_LABEL_REGEX
			+ "\\.)+" + "(" + TOP_LABEL_REGEX + ")$";
	private static final String regex = "/fr/croisieres/[\\w-]*/destination,([0-9]*),([0-9]*)/?(.*)$";
	private static final String SUBDOMAIN_PATTERN = "(?:(?!www).)*\\.(?:abcroisiere|crocierepromo|crucerospromo|promokreuzfahrten).*/([a-zA-Z_]{2,20})/.*$";

	private static final String domainPattern = "^http://([^/]+)/";
	private static final String urlCDF ="http://m.croisieresdefrance.abcroisiere.com/";
	private static final String urlMSC ="http://m.msc.abcroisiere.com/";
	private static final String urlABC = "http://www.abcroisiere.com/";

	private static final List<String> urls = Arrays.asList(
			"http://maps.google.com/maps",
			"http://www.google.com/flights/gwsredirect",
			"http://slav0nic.org.ua/static/books/python/",
			"http://webcache.googleusercontent.com/search",
			"http://www.python.org/ftp/python/doc/",
			"http://webcache.googleusercontent.com/search",
			"http://www.python.org/ftp/python/");

	private static final Pattern p = Pattern.compile("^http://([^/]+)/");

	private static final int TRIES = 50000;

	public static void main(final String... args) {
		
		// autre methode
		autreMethode();
		
		//
//		String domain = getSubdomain(urlABC);
//		System.out.println(">>>"+domain);
//		
//		
//		
//		for (final String url : urls)
//			System.out.printf("%s: %b\n", url, regexIsOK(url));
//
//		long start, end;
//
//		start = System.currentTimeMillis();
//		for (int i = 0; i < TRIES; i++)
//			for (final String url : urls)
//				regexIsOK(url);
//		end = System.currentTimeMillis();
//
//		System.out.println("Time taken: " + (end - start) + " ms");
//		System.exit(0);
	}

	private static boolean regexIsOK(final String url) {
		final Matcher m = p.matcher(url);

		return m.find() && !m.group(1).contains("google");
	}

	/**
	 * @param s
	 * @param pattern
	 * @return
	 */
	private static boolean IsMatch(String s, String pattern) {
		try {
			Pattern patt = Pattern.compile(pattern);
			Matcher matcher = patt.matcher(s);
			// System.out.println("groupe1: "+matcher.group(1));
			return matcher.matches();
		} catch (RuntimeException e) {
			return false;
		}
	}

	private void Split(String url) {
		System.out.println("SPLIT");
		Pattern pattern = Pattern.compile("(https?://)([^:^/]*)(:\\d*)?(.*)?");
		Matcher matcher = pattern.matcher(url);

		matcher.find();

		String protocol = matcher.group(1);
		String domain = matcher.group(2);
		String port = matcher.group(3);
		String uri = matcher.group(4);

		System.out.println(matcher.group(1));
		System.out.println(matcher.group(2));
		System.out.println(matcher.group(3));
		System.out.println(matcher.group(4));
	}

	private static void autreMethode(){
		String resultString = null; 
		Pattern regex = Pattern.compile("(http:\\/\\/)?(www.)?m.(msc|croisieresdefrance)+.abcroisiere.*/.*$");
		String url ="http://p3.m.croisieresdefrance.abcroisiere.com.local:8080/";
		Matcher regexMatcher = regex.matcher(url);
		if(regexMatcher.find()){
			resultString = regexMatcher. group(3);
			System.out.printf("url>>>> %s \n",resultString);
		}
	}
	
	private static String getSubdomain(String url) {
        final String[] serverParts = url.split("\\.");
        if(serverParts.length > 2) {
                String subdomain = "";
                final int length = serverParts.length - 2;
                for(int i = 0 ; i < length; i++) {
                        subdomain += serverParts[i];
                        if(i < length - 1)
                                subdomain += ".";
                }
                return subdomain;
        }
        
        throw new IllegalStateException("No subdomain found");
}
}